package com.dmzc.sort;

//冒泡排序
public class BuddleSort {
    public static void main(String[] args) {
        Node1[] node1s = new Node1[5];
        Node1 node1 = new Node1(5, 6);
        node1s[0] = node1;
        node1 = new Node1(5, 5);
        node1s[1] = node1;
        node1 = new Node1(1, 3);
        node1s[2] = node1;
        node1 = new Node1(3, 2);
        node1s[3] = node1;
        node1 = new Node1(2, 1);
        node1s[4] = node1;
        int i = buddleSort(node1s);
        System.out.println(i);
        for(int j=0;j<node1s.length;j++){
         System.out.println(node1s[j]);
        }
    }

    public static int buddleSort(Node1[] arr) {
        if (arr.length == 0) return 0;
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j].index > arr[j + 1].index) {
                    Node1 tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = true;
                    count++;
                }
            }
            if (!flag) {

                return count;
            }
        }
        return count;
    }
}
